package com.time.company.components.navigation.impl;

import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.time.company.components.navigation.INavigation;


/**
 * Created by Bing on 2016/8/23.
 * E-mail is tx16bing@163.com
 */
public abstract class AbsNavigation<P extends AbsNavigation.Builder.NavigationParams> implements INavigation {

    private View contentView;
    private P params;

    // 构造器， 传递构建参数
    protected AbsNavigation(P params) {
        this.params = params;
    }

    protected View findViewById(int viewId) {
        return contentView.findViewById(viewId);
    }

    public String getString(int textRes) {
        if (textRes == 0) {
            return null;
        }
        return this.params.context.getResources().getString(textRes);
    }

    public int getColor(int colorRes) {
        return ContextCompat.getColor(this.params.context, colorRes);
    }

    public P getParams() {
        return params;
    }

    @Override
    public void createAndBind() {
        // 构建布局
        contentView = LayoutInflater.from(params.context).inflate(getLayoutId(), params.parent, false);
        ViewGroup viewGroup = (ViewGroup) contentView.getParent();
        if (viewGroup != null) {
            viewGroup.removeView(contentView);
        }
        params.parent.addView(contentView, 0);
    }

    // Builder 构建器
    public static abstract class Builder {

        public abstract AbsNavigation create();

        public static class NavigationParams {
            // 上下文
            public Context context;
            // 父容器
            public ViewGroup parent;

            public NavigationParams(Context context, ViewGroup parent) {
                this.context = context;
                this.parent = parent;
            }
        }
    }

}
